<?php
require_once '../entity/Masker.php';
require_once '../entity/Question.php';
require_once 'DaoUtils.php';
require_once 'MaskerDao.php';
require_once 'NotificationDao.php';
class QuestionDao extends DaoUtils {
	//FIXME nosense pass the $textAnswer here!
	public function insertQuestion($idMaskerFrom, $nameMaskerTo, $signId, $text, $textAnswer) {
		$text = $this -> escape($text);
		$textAnswer = $this -> escape($textAnswer);
		
		$maskerDao = new MaskerDao();
		$masker = $maskerDao->getMaskerByName($nameMaskerTo);
		$idMaskerTo = $masker["id"];
		
		if($signId == null) {
			$signId = "null";
		}
		
		$query = "insert into " . Question::getTableName() . " (id_masker, id_masker_to, id_sign, text, text_answer) ";
		$query .= " values (" . $idMaskerFrom . "," . $idMaskerTo . "," . $signId . ",'" . $text . "','" . $textAnswer . "') ";
	
		$result = $this -> insert($query);
			
		$notificationDao = new NotificationDao();
		$notificationDao->insertNotificationWithText($idMaskerFrom, $idMaskerTo, NotificationType::$question, $text);

		return $result;
	}
	
	public function updateQuestionAnswer($idQuestion, $idMasker, $textAnswer) {
		$textAnswer = $this -> escape($textAnswer);
		
		$query = "update " . Question::getTableName() . " set text_answer = '" . $textAnswer . "' ";
		$query .= " where id_masker_to = " . $idMasker . " and id = " . $idQuestion;
	
	
		$result = $this -> update($query);
		
		
		$notificationDao = new NotificationDao();
		$question = $this->getQuestion($idQuestion);
		$notificationDao->insertNotificationWithText($idMasker, $question->id_masker, NotificationType::$questionReply, $textAnswer);

		return $result;
	}
	
	public function deleteQuestion($idQuestion, $idMasker) {
		$query = "update " . Question::getTableName() . " set visible = 'N' ";
		$query .= " where id_masker_to = " . $idMasker . " and id = " . $idQuestion;
	
		$result = $this -> update($query);

		return $result;
	}
	
	
	public function getMaskerQuestion($maskerName, $lastId) {
		return $this ->getQuestionList($lastId, " and q.id_masker_to IN (SELECT id FROM " . Masker::getTableName() . " WHERE name = '".$maskerName."') ");
	}
	
	
	private function getQuestionList($lastId, $where) {
		if(isset($lastId) && $lastId != "") {
			$where .= " and q.id < ".$lastId;
		}
		
		$query =  " SELECT q.id,q.id_masker,q.id_masker_to,q.id_sign,q.text,q.insert_date,q.text_answer,m.name masker_name ";
		$query .= " FROM " . Question::getTableName() . " q JOIN " . Masker::getTableName() . " m ON q.id_masker = m.id ";
		$query .= " WHERE visible = 'Y' ".$where." order by q.id desc";

		$records = $this -> selectRecords($query);
		$questionArray = array();
		
		foreach ($records as $record)  {
			$question = new Question();
			$question -> setQuestion($record['id'],$record['id_masker'], $record['id_masker_to'], $record['text_answer'],
			 $record['id_sign'], $record['insert_date'], $record['text'], $record['masker_name']);
			 
			array_push($questionArray,$question);
		} 
		return $questionArray;
	}
	private function getQuestion($id) {
		$query =  " SELECT q.id,q.id_masker,q.id_masker_to,q.id_sign,q.text,q.insert_date,q.text_answer,m.name masker_name ";
		$query .= " FROM " . Question::getTableName() . " q JOIN " . Masker::getTableName() . " m ON q.id_masker = m.id ";
		$query .= " WHERE q.id = ".$id." order by q.id desc";

		$record = $this -> selectRecord($query);
		
		$question = new Question();
		if ($record != null) {
			$question -> setQuestion($record['id'],$record['id_masker'], $record['id_masker_to'], $record['text_answer'],
			$record['id_sign'], $record['insert_date'], $record['text'], $record['masker_name']);
		} 
		
		return $question;
	}
}
?>